package com.github.glomadrian.codeinputlib;

import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class Logger {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 110, "seagazer");

    private static String replaceFormat(String logMessageFormat) {
        return logMessageFormat.replaceAll("%([d|f|s])", "%{public}$1");
    }

    /**
     * 信息info
     * @param message
     * @param args
     **/
    public static void i(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.info(LABEL, replaceFormat(message), args);
        }
    }

    /**
     * 信息debug
     * @param message
     * @param args
     **/
    public static void d(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.debug(LABEL, replaceFormat(message), args);
        }
    }

    /**
     * 信息warn
     * @param message
     * @param args
     **/
    public static void w(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.warn(LABEL, replaceFormat(message), args);
        }
    }

    /**
     * 信息error
     * @param message
     * @param args
     **/
    public static void e(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.error(LABEL, replaceFormat(message), args);
        }
    }

    /**
     * 信息fatal
     * @param message
     * @param args
     **/
    public static void f(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.fatal(LABEL, replaceFormat(message), args);
        }
    }
}
